#!/usr/bin/python

from robbus import Robbus

def main(argv = None):
	if len(argv) < 3:
		print "Usage: test.py address data [mask]"
		print "All address, data and mask are read as hexa values if"
		print "prefixed with \\x (or \\\\x to avoid shell expansion)"
		print "Example (bash): ./test.py t \\\\x0305"
		return
	
	robbus = Robbus()
	robbus.open('/dev/robbus', 9600)
	if argv[1][0:2] == "\\x":
		print "Decoding hex address"
		address = argv[1][2:].decode("hex");
	else:
		address = argv[1];
	if len(address) != 1:
		print "Address must have 1 byte"
		return
	if argv[2][0:2] == "\\x":
		print "Decoding hex data", argv[2]
		data = argv[2][2:].decode("hex");
	else:
		data = argv[2];
	if len(argv) > 3:
		if argv[3][0:2] == "\\x":
			print "Decoding hex mask"
			mask = argv[3][2:].decode("hex");
		else:
			mask = argv[3];
		if len(mask) != 1:
			print "Mask must have 1 byte"
			return
	else:
		mask = None
	robbus.send(address,data)
	robbus.close()

if __name__ == "__main__":
	from sys import argv
	main(argv)
